import string
import random

from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models import Max
from django.utils import timezone


# Create your models here.

class ClassModel(models.Model):
    name = models.CharField(max_length=20, verbose_name='班级名称')
    def __str__(self):
        return self.name
    class Meta:
        verbose_name = '班级'
        verbose_name_plural = '班级'


class CustomUser(AbstractUser):
    # 添加角色字段
    is_student = models.BooleanField(default=False)
    is_teacher = models.BooleanField(default=False)
    # 添加通用字段（如果需要）
    # ...
def generate_random_student_number():
    random_digits = ''.join(random.choice(string.digits) for _ in range(6))
    random_letters = ''.join(random.choice(string.ascii_uppercase) for _ in range(3))
    return random_digits + random_letters


def generate_random_teacher_number():
    random_digits = ''.join(random.choice(string.digits) for _ in range(6))
    return random_digits


class StudentProfile(models.Model):
    user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, primary_key=True)
    student_id = models.CharField(max_length=9, default=generate_random_student_number)
    classname = models.ForeignKey(ClassModel, on_delete=models.CASCADE, verbose_name='班级', null=True, blank=True)

class TeacherProfile(models.Model):
    user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, primary_key=True)
    teacher_id = models.CharField(max_length=6, default=generate_random_teacher_number, unique=True)
    phone = models.CharField(max_length=11, verbose_name='手机号', unique=True)
    profession = models.CharField(max_length=20, verbose_name='职称')

class CourseModel(models.Model):
    name = models.CharField(max_length=20, verbose_name='课程名称')
    study_time = models.IntegerField(verbose_name='学时')
    def __str__(self):
        return self.name
    class Meta:
        verbose_name = '课程'
        verbose_name_plural = '课程'

class TextbookModel(models.Model):
    name = models.CharField(max_length=20, verbose_name='教材名称')
    price = models.FloatField(verbose_name='价格')

    def __str__(self):
        return self.name
    class Meta:
        verbose_name = '教材'
        verbose_name_plural = '教材'
# 授课
class CourseTeacher(models.Model):
    course = models.ForeignKey(CourseModel, on_delete=models.CASCADE, verbose_name='课程')
    teacher = models.ForeignKey(TeacherProfile, on_delete=models.CASCADE, verbose_name='教师')
    classname = models.ForeignKey(ClassModel, on_delete=models.CASCADE, verbose_name='班级')
    textbook = models.ForeignKey(TextbookModel, on_delete=models.CASCADE, verbose_name='教材')
    school_year = models.CharField(max_length=32, verbose_name='学年')
    semester = models.CharField(max_length=32, verbose_name='学期')
    content = models.TextField(verbose_name='介绍内容')
    def __str__(self):
        return f'{self.course.name} - {self.teacher.user.username}'
    class Meta:
        verbose_name = '课程教师'
        verbose_name_plural = '课程教师'


# 验证码
class CaptchaModel(models.Model):
    email = models.EmailField(unique=True)
    captcha = models.CharField(max_length=6)
    created_time = models.DateTimeField(auto_now_add=True)



class UserModel(models.Model):
    username = models.CharField(max_length=20, unique=True)
    password = models.CharField(max_length=20)
    email = models.EmailField(unique=True)
    created_time = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.username
    class Meta:
        verbose_name = '用户'
        verbose_name_plural = '用户'

def generate_random_invited_number():
    random_digits = ''.join(random.choice(string.digits) for _ in range(6))
    return random_digits


# 邀请码
class InviteCodeModel(models.Model):
    code = models.CharField(max_length=6, default=generate_random_invited_number, unique=True)
    created_time = models.DateTimeField(auto_now_add=True)
    used = models.BooleanField(default=False)
    def __str__(self):
        return self.code
    class Meta:
        verbose_name = '邀请码'
        verbose_name_plural = '邀请码'